/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: AppDateilController
 * Author:   Allen
 * Date:     2020/8/23 14:20
 * Description: 开放平台生成密钥处理Controller
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.rest.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bim.marvel.openapi.client.dto.AppDetailDTO;
import com.bim.marvel.openapi.client.query.PageQuery;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.core.model.dto.SimplePageVO;
import com.bim.marvel.common.core.model.dto.SimpleVO;
import com.bim.marvel.common.util.SimpleConverter;
import com.bim.marvel.openapi.core.entity.AppDetailDO;
import com.bim.marvel.openapi.core.service.impl.AppDetailService;
import com.bim.marvel.rest.AppDateilController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈开放平台生成密钥处理Controller〉
 *
 * @author Allen
 * @date 2020/8/23
 * @since 1.0.0
 */
@RestController
public class AppDateilControllerImpl  implements AppDateilController {

    /**
     * 注入AppDetailService
     */
    @Autowired
    private AppDetailService appDetailService;



    /**
     * 删除开放平台密钥信息
     * @param appId AppId信息
     * @return 是否操作成功标识
     */
    @Override
    @CacheEvict(value = "getAppDetailInfo", key = "'appInfo:' + #appId")
    public SimpleVO<Boolean> removeById(String appId) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = appDetailService.removeById(appId);
        if (!success) {
            return new SimpleVO(CodeEnum.DELETE_FAILED);
        }
        return simpleVO;
    }

    @Override
    @CachePut(value = "getAppDetailInfo", key = "'appInfo:' + #appId")
    public SimpleVO<AppDetailDTO> updateTokenById(String appId, String token) {
        AppDetailDO appDetailDO = new AppDetailDO();
        appDetailDO.setAppId(appId);
        appDetailDO.setToken(token);
        boolean success = appDetailService.updateById(appDetailDO);
        if (!success) {
            return new SimpleVO(CodeEnum.UPDATE_FAILED);
        }
        //为了更新缓存数据
        appDetailDO = appDetailService.getById(appId);
        AppDetailDTO appDetailDTO = new AppDetailDTO();
        BeanUtils.copyProperties(appDetailDO, appDetailDTO);
        return new SimpleVO<>(appDetailDTO);
    }

    @Override
    @Cacheable(value = "getAppDetailInfo", key = "'appInfo:' + #appId")
    public SimpleVO<AppDetailDTO> getAppDetail(String appId) {
        AppDetailDO appDetailDO = appDetailService.getById(appId);
        if (appDetailDO == null) {
            return new SimpleVO(CodeEnum.DATA_NOT_FOUND);
        }
        AppDetailDTO appDetailDTO = new AppDetailDTO();
        BeanUtils.copyProperties(appDetailDO, appDetailDTO);
        return new SimpleVO<>(appDetailDTO);
    }

    @Override
    public SimplePageVO<List<AppDetailDTO>> listAppDetail(String appId, PageQuery pageQuery) {
        Page page = new Page(pageQuery.getPageIndex(), pageQuery.getPageSize());
        AppDetailDO appDetailDO = new AppDetailDO();
        appDetailDO.setAppId(appId);
        QueryWrapper<AppDetailDO> queryWrapper = new QueryWrapper<>(appDetailDO);
        IPage<AppDetailDO> appDetailDOIPage = appDetailService.page(page, queryWrapper);
        List<AppDetailDO> appDetailDOList = appDetailDOIPage.getRecords();
        if (CollectionUtils.isEmpty(appDetailDOList)) {
            return new SimplePageVO<>(CodeEnum.DATA_NOT_FOUND);
        }
        List<AppDetailDTO> appDetailDTOList = SimpleConverter.convert(appDetailDOList, AppDetailDTO.class);
        return new SimplePageVO(appDetailDTOList, appDetailDOIPage);
    }


}
